package com.dminvoicemanager.tabs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dminvoicemanager.DM_Retailer_Master;
import com.dminvoicemanager.R;

public class DM_Retailer_Information extends Activity {
	private static final String TAG = null;
	EditText dmretcode, dmrettown, dmretaddress, dmretaddressa, dmretaddressb,
			etrettelephone, dmretmobile, etrefemail, dmretterritory,
			dmretstatus, dmretoutdis;
	SQLiteDatabase mydb;
	String TagIdRetailer, printStatus;
	private String DBNAME = "dminvoicemanager.db";
	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(),
				DM_Retailer_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_retailer_information);

		callCurrentRetailer();
		/*
		 * Bundle bundle = getIntent().getExtras(); TagIdRetailer =
		 * bundle.getString("TAGIDRET");
		 */
		Log.w(TAG, "TagIdRetailer:" + TagIdRetailer);

		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Retailer Information");
		dmretcode = (EditText) findViewById(R.id.dmretcode);
		dmrettown = (EditText) findViewById(R.id.dmrettown);
		dmretaddress = (EditText) findViewById(R.id.dmretaddress);
		dmretaddressa = (EditText) findViewById(R.id.dmretaddressa);
		dmretaddressb = (EditText) findViewById(R.id.dmretaddressb);
		etrettelephone = (EditText) findViewById(R.id.etrettelephone);
		dmretmobile = (EditText) findViewById(R.id.dmretmobile);
		etrefemail = (EditText) findViewById(R.id.etrefemail);
		dmretterritory = (EditText) findViewById(R.id.dmretterritory);
		dmretstatus = (EditText) findViewById(R.id.dmretstatus);
		dmretoutdis = (EditText) findViewById(R.id.dmretoutdis);
		displayRetailerInformation();
	}

	private void callCurrentRetailer() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentRetailer = null;
		try {
			String query = "SELECT * from FRetailScrSum";
			curCurrentRetailer = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentRetailer != null) {
			if (curCurrentRetailer.getCount() > 0) {
				while (curCurrentRetailer.moveToNext()) {
					TagIdRetailer = curCurrentRetailer.getString(1);
				}
			}
		}
	}

	private void displayRetailerInformation() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curRetailerInfo = null;

		try {
			String query = "SELECT DB.DebCode,DB.DebName,DB.DebAdd1,DB.DebAdd2,DB.DebAdd3,DB.DebTele,DB.DebMob,DB.DebEMail,DB.TownCode,DB.OutDis,DB.Status,DB.AreaCode,TW.TownCode,TW.TownName,AR.AreaCode,AR.AreaName from fDebtor DB,fTown TW,fArea AR Where DB.TownCode = TW.TownCode AND DB.AreaCode = AR.AreaCode AND DB.DebCode = '"
					+ TagIdRetailer + "'";
			curRetailerInfo = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curRetailerInfo != null) {
			if (curRetailerInfo.getCount() > 0) {
				while (curRetailerInfo.moveToNext()) {
					String DebCode = curRetailerInfo.getString(0);
					String TownName = curRetailerInfo.getString(13);
					String DebAdd1 = curRetailerInfo.getString(2);
					String DebAdd2 = curRetailerInfo.getString(3);
					String DebAdd3 = curRetailerInfo.getString(4);
					String DebTele = curRetailerInfo.getString(5);
					String DebMob = curRetailerInfo.getString(6);
					String DebEMail = curRetailerInfo.getString(7);
					String AreaName = curRetailerInfo.getString(15);
					String Status = curRetailerInfo.getString(10);
					if (Status.equals("A")) {
						printStatus = "Active";
					}
					else{
						printStatus = "Inactive";
					}
					String OutDis = curRetailerInfo.getString(9);

					dmretcode.setText(DebCode);
					dmrettown.setText(TownName);
					dmretaddress.setText(DebAdd1);
					dmretaddressa.setText(DebAdd2);
					dmretaddressb.setText(DebAdd3);
					etrettelephone.setText(DebTele);
					dmretmobile.setText(DebMob);
					etrefemail.setText(DebEMail);
					dmretterritory.setText(AreaName);
					dmretstatus.setText(printStatus);
					dmretoutdis.setText(OutDis);
				}
			}
		}
		curRetailerInfo.close();
		mydb.close();
	}
	
	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
